package com.gfm.story.controller;

import com.gfm.story.entity.base.PageBean;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.user.User;
import com.gfm.story.interceptor.UserContext;
import com.gfm.story.service.story.RecommendService;
import com.gfm.story.service.user.UserService;
import com.gfm.story.util.ParamUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.controller
 * @Author: Administrator
 * @CreateTime: 2019-06-04 09:44
 * @Description: 小说推荐接口
 */
@RequestMapping("recommend")
@Controller
public class RecommendController {

    @Resource
    private RecommendService recommendService;
    @Resource
    private UserService userService;


    @RequestMapping("getRecommendPage")
    public String getRecommendPage(HttpServletRequest request,int index, PageBean pageBean, ModelMap modelMap) throws Exception {
        Map<String, Object> map = ParamUtil.getParameterMap(request);
        String date = request.getParameter("date");//榜单类型
        if (date!=null){
            map.put("date",date);
        }
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getRows());
        modelMap.put("bookList",recommendService.getBookListByDay(map,pageBean.getPage()));
        modelMap.put("index",index);
        return "ph";
    }


    @RequestMapping("addOrEdit")
    @ResponseBody
    public ResultBean addOrEdit(Long bookId,Long userId){
        if (bookId==null){
            return ResultBean.getSuccess("推荐失败");
        }
        User user = UserContext.getUser();
        if (user==null){
            return ResultBean.getSuccess("推荐失败,请先登录");
        }else{
            user=userService.getUserBySession(user);
            if (recommendService.addOrEdit(bookId,user.getId())){
                return ResultBean.getSuccess("推荐成功");
            }
            return ResultBean.getSuccess("推荐失败,您今日已经推荐过一次了!");
        }
    }

}
